package com.ymttest.business.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymt.base.YmatouBaseCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.TestDataManager;

public class WikiSpide {
	public static void main(String args[]) throws Exception {
		List<String> data=new ArrayList<>();
		//wiki登入
		String loginurl="http://wiki.ymatou.cn:8090/dologin.action";
		String loginp="os_username=zhouyi&os_password=Welcome#890&login=zhouyi";
		YmatouBaseCall logincall = new YmatouBaseCall(loginurl,"", "POST", "STRING");
		logincall.setData(loginp);
		logincall.callService();
		//查询自动化测试对应的左边treepage
		//http://wiki.ymatou.cn:8090/pages/viewpage.action?pageId=26149138&src=contextnavpagetreemode
		//需要修改pageid 26149138
		String url = "http://wiki.ymatou.cn:8090/plugins/pagetree/naturalchildren.action?decorator=none&excerpt=false&sort=position&reverse=false&disableLinks=false&expandCurrent=true&hasRoot=true&pageId=26149138&treeId=0&startDepth=0&mobile=false&ancestors=10256537&treePageId=22121229";
		YmatouBaseCall ec = new YmatouBaseCall(url, "","GET", "PARAM");
		ec.setCookies(logincall.getCookies());
		ec.callService();
		String html1=ec.getReturnData();
		//截取工作链接
		List<String> list = TestDataManager.getRegexList(html1, "/display/AUTOWIKI/2018.*?\"");
		for (String string : list) {
			String baseurl="http://wiki.ymatou.cn:8090/";
			baseurl+=string.substring(0,string.length()-1)+"?src=contextnavpagetreemode";
			ec = new YmatouBaseCall(baseurl, "","GET", "PARAM");
			ec.setCookies(logincall.getCookies());
			ec.callService();
			List<String> tablelist = TestDataManager.getRegexList(ec.getReturnData(), "<table class=\"confluenceTable\">.*</table>");
			for (String stable : tablelist) {
				List<String> trlist = TestDataManager.getRegexList(stable, "<tr>.*?</tr>");
				for (String str : trlist) {
					//前置零宽不能支持这个匹配 只能匹配后再截取到>
					List<String> tdlist = TestDataManager.getRegexList(str, "<td[^>]*>.*?(?=</td>)");
					String e="";
					for (String std : tdlist) {
						std=std.substring(std.indexOf('>')+1);
						e+=std+":::";
					}
					data.add(e);
				}
			}
		}
		for (String mydata : data) {
			if(mydata.contains("周忆")){
				String[] d = mydata.split(":::");
				TestDataManager.appendFileToLine("work.txt", d[1]);
			}
		}
		
	}
}
